package cn.christ.com.mobilesafe.common;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.umeng.analytics.MobclickAgent;

import butterknife.ButterKnife;
import cn.christ.com.mobilesafe.R;

/**
 * Created by Christ on 2018/7/13.
 * Activity基类，所有的activity都需要继承此类。
 * toast,showDialog,showProgressDialog等方法
 */

public abstract class BaseActivity extends AppCompatActivity implements IUIOperation {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutRes());
        ButterKnife.bind(this);

        Global.setNoStatusBarFullMode(this);
        View tvSetingTitle = findViewById(R.id.tv_seting_title);
        if (tvSetingTitle != null) {
            Global.setStatusBarPadding(tvSetingTitle);
        }
        initPresenter();
        initView();
        initListener();
        initData();
    }

    public abstract void initPresenter();

    @Override
    protected void onResume() {
        super.onResume();
        //友盟
        MobclickAgent.onResume(this);
    }


    public void initView() {

    }

    @Override
    protected void onPause() {
        super.onPause();
        //友盟
        MobclickAgent.onPause(this);
    }

    /**
     * 不需要强转
     *
     * @param id
     * @param <T>
     * @return
     */
    public <T> T findView(int id) {
        T view = (T) findViewById(id);
        return view;
    }


    /**
     * 在主线程或子线程中都可以使用Toast
     *
     * @param text
     */
    public void showToast(String text) {
        Global.showToast(text);
    }

    private ProgressDialog progressDialog;

    /**
     * 展示进度条对话框
     *
     * @param messageg
     */
    protected void showProggressDialog(final String messageg) {
        Global.runOnMainThread(new Runnable() {
            @Override
            public void run() {
                progressDialog = new ProgressDialog(BaseActivity.this);
                progressDialog.setMessage(messageg);
                //点击外部时不销毁
                progressDialog.setCanceledOnTouchOutside(false);
                //activity如果正在销毁或已销毁，不能showDialog,否则出错。
                if (!isFinishing()) {
                    progressDialog.show();
                }
            }
        });
    }

    /**
     * 销毁加载对话框
     */
    protected void dismissProgressDialog() {
        if (progressDialog != null) {
            progressDialog = null;
        }
    }

    /**
     * 显示对话框
     *
     * @param title    标题
     * @param message  内容
     * @param listener 回调监听器
     */
    protected void showDialog(String title, String message, final OnDialogClickListener listener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(title);
        builder.setMessage(title);
        builder.setMessage(message);
        builder.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int i) {
                if (listener != null) {
                    listener.onConfirm(dialog);
                }
            }
        });
        builder.setNegativeButton(R.string.cancle, null);
        if (!isFinishing()) {
            builder.create().show();
        }
    }

    /**
     * 显示对话框
     *
     * @param title
     * @param messagge
     */
    protected void showDialog(String title, String messagge) {
        showDialog("", messagge, null);
    }

    /**
     * 显示对话框
     *
     * @param message
     */
    protected void showDialog(String message) {
        showDialog("", message, null);
    }

    public interface OnDialogClickListener {
        //确定
        void onConfirm(DialogInterface dialog);

        //取消
        void onCancel(DialogInterface dialog);
    }


    /**
     * 跳转activity
     *
     * @param clazz
     */
    protected void startActivity(Class clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
        finish();
    }

    /**
     * 跳转activity,不finish
     *
     * @param clazz
     */
    protected void startActivityNotFinish(Class clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
    }

}
